/**
 * 水泵历史记录
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require("underscore");
const decimal = require("decimal");
const {timeComponent,chartDataProcessing} = require("../../libs/Utils")
const columnNameObj = require("./deviceChartConfig").chart.waterPump;
const deviceType = require("./deviceChartConfig").type.waterPump;

module.exports = async function (params) {
    try{
    let timeInterval = timeComponent(params.startTime,params.endTime,params.type);
    let sql = `SELECT
        dd.device_no AS deviceNo,
        DATE_FORMAT(dd.data_time,"%Y-%m-%d %H:%i:%s") AS dataTime,
        ${timeInterval.queryGroupTime} 
        df.device_name AS deviceName,
        dd.runtime,
        dd.status,
        dd.fault_type AS faultType,
        dd.start_count AS startCount,
        rf.range_name AS rangeName
    FROM
    data_water_pump_${params.userDomainId} dd
    INNER JOIN device_file df ON df.device_no = dd.device_no
    AND df.delete_sign = 1 AND df.device_type = ${deviceType}
    AND df.range_id IN (?)
    INNER JOIN range_file rf ON df.range_id = rf.range_id AND rf.delete_sign = 1 
    WHERE 1 = 1 ${timeInterval.startTime && timeInterval.endTime ? ` AND (dd.data_time > "${timeInterval.startTime}" AND dd.data_time <= "${timeInterval.endTime}")` : `` } 
    ORDER BY dd.data_time DESC`;
    let waterPumpDataList = await conn(sql,[params.rangeIds]);
    let waterPumpDataGroup = _.groupBy(waterPumpDataList,"deviceNo");
    let dataList = [];
    for(const deviceNo in waterPumpDataGroup){
        let deviceDataList = waterPumpDataGroup[deviceNo];
        let deviceObj = {
            deviceName:deviceDataList[0].deviceName,
            rangeName:deviceDataList[0].rangeName,
            deviceNo:deviceNo,
            runtime:deviceDataList[0].runtime,
            faultType:deviceDataList[0].faultType,
            status:deviceDataList[0].status,
            startCount:0,
            dataList:[]
        }
        deviceObj.startCount = _.reduce(deviceDataList, function(memo, num){
            return decimal.add(memo,num.startCount).internal;
        },0)
        if(params.type === "hour"){
            deviceObj.dataList = deviceDataList
        }else {
            let timeGroup = _.groupBy(deviceDataList,"groupTime");
            for(const time in timeGroup){
                let timeGroupList = timeGroup[time];
                let deviceDataObj = {
                    dataTime:time,
                    dataCreateTime:timeGroupList[timeGroupList.length - 1].dataTime,
                    deviceName:deviceDataList[0].deviceName,
                    rangeName:deviceDataList[0].rangeName,
                    deviceNo:deviceNo,
                    runtime:deviceDataList[0].runtime,
                    faultType:deviceDataList[0].faultType,
                    status:deviceDataList[0].status,
                    startCount:0,
                }
                deviceDataObj.startCount = _.reduce(timeGroupList, function(memo, num){ return decimal.add(memo,num.startCount).internal; },0)
                deviceObj.dataList.push(deviceDataObj);
            }
        }
        deviceObj.chart = chartDataProcessing(deviceObj.dataList,columnNameObj);
        dataList.push(deviceObj);
    }
    return { success: true,dataList:dataList};
    }catch (e){
        return { success: false,dataList:[],message:e.code === "ER_NO_SUCH_TABLE" ? "当前账号无此类设备" : e.sqlMessage};
    }
};